<template>
  <!-- <div>基础模块</div> -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="btnCannel">
    <!-- 表单数据 -->
    <el-form ref="deptForm" :model="formDate" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formDate.name" style="width:80%;"/>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formDate.code" style="width:80%;"/>
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <!-- native修饰符 找到原生元素的事件 -->
        <el-select v-model="formDate.manager" style="width:80%;" @focus="getEmployeeSimple">
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label=" item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formDate.introduce"
          type="textarea"
          style="width:80%;"
          placeholder="1-300个字符"
        />
      </el-form-item>
    </el-form>
    <!-- 确定消息 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCannel">取消</el-button>
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
  <!-- 弹窗 -->
</template>

<script>
import {
  getDepartments,
  addDepartment,
  getDeparDetail,
  updateDepartments
} from '@/api/departments'
// import { getEmployeeSimple } from '@/api/employess'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // 当前操作的节点
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    // 检查部门名称是否重复
    const checkNameRepeat = async (rule, value, callback) => {
      // value是部门名称 要去和同级部门下的部门去比较 有没有相同的  有相同的不能过
      const { depts } = await getDepartments()
      // 去找统计部门下 有没有和value相同的数据
      // 找到所有子部门
      let isRepeat = false
      if (this.formDate.id) {
        // 有id 就是编辑模式
        isRepeat = depts
          .filter(
            item =>
              item.pid === this.treeNode.pid && item.id !== this.treeNode.id
          )
          .some(item => item.name === value)
      } else {
        // 没有id  新增模式
        isRepeat = depts
          .filter(item => item.pid === this.treeNode.id)
          .some(item => item.name === value)
      }
      isRepeat
        ? callback(new Error(`同级部门下已经存在这个${value}部门了`))
        : callback()
    }
    const checkCodeRepeat = async (rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formDate.id) {
        isRepeat = depts.some(
          item => item.id !== this.treeNode.id && item.code === value && value
        )
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }
      isRepeat
        ? callback(new Error(`部门编码下已经存在这个${value}编码了`))
        : callback()
    }
    return {
      peoples: [],
      // showDialog: false,
      // 表单数据
      formDate: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称长度为1~50个字符',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: checkNameRepeat
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码长度为1~50个字符',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: checkCodeRepeat
          }
        ],
        manager: [
          { required: true, message: '部门列表不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍长度为1~300个字符',
            trigger: 'blur'
          }
        ]
      } // 校验规则
    }
  },
  computed: {
    showTitle() {
      return this.formDate.id ? '编辑部门' : '新增子部门'
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    // 获取部门详情
    async getDeparDetail(id) {
      this.formDate = await getDeparDetail(id)
    },
    // 确定按钮
    btnOK() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          if (this.formDate.id) {
            // 编辑
            await updateDepartments(this.formDate)
          } else {
            await addDepartment({
              ...this.formDate,
              pid: this.treeNode.id
            })
          }
          // 表示可以提交了
          // 调用新增接口 添加父部门的id
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 关闭弹层
    btnCannel() {
      // resetFields只能重置表单上的数据 非表单  比如编辑
      this.formDate = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:showDialog', false)
      // 重置表单
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>
<style lang='less' scoped>
</style>
